# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import fields, osv
from openerp.addons.dm_rubylong import get_rubylong_fields_xml, get_rublong_data_url
from openerp.addons.dm_base.utils import field_selection_name as selection_name
from openerp.addons.dm_base.utils import field_boolean_name_cn as boolean_name
from datetime import datetime
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT

class mrp_plan_day_print(osv.osv_memory):
    
    _name="mrp.plan.day.print"
        
    def _get_rubylong_xml_file(self, cr, uid, ids, field_names, args, context=None):
        if isinstance(ids, (int, long)):
            ids = [ids]
        res = {}
        for order_id in ids:
            res[order_id] = ''
        data_xml = ''
        for order in self.browse(cr, uid, ids, context=context):
            data_xml = ''
            #header data
            order_fields = [
                        ('company_id.logo','company_logo'),
                        ('company_id.name','company_id_name'),
                        #排程日期
                        ('date_plan','create_date'),
                        #工作中心
                        ('workcenter_id.name','workcenter_id'),
                        
                        ('id','order_id'),
                        ]
            data_xml += get_rubylong_fields_xml(order, 'header', order_fields)
            
            #date
            ids_schedule_line = self.pool.get('mrp.wo.day.schedule.line').search(cr, uid, [('date_schedule', '=', order.date_plan),('workcenter_id','=',order.workcenter_id.id)], context=context)
            seq = 0
            #detail data
            for line in self.pool.get('mrp.wo.day.schedule.line').browse(cr, uid, ids_schedule_line, context=context):
                line_fields = [
                            #工单
                            ('wo_id.code','code'),
                            ('wo_id.name','wo_id'),
                            #产品
                            ('product_id.default_code','default_code'),
                            ('product_id.name','product_id'),
                            #工序
                            ('oper_id.name','oper_id'),
                            #计划量
                            ('qty_schedule','qty_plan'),
                            
                            ('product_id.id','order_id'),
                            ]
                
                seq += 1
                line_fields_new = line_fields + [{'order_line':seq}]
                
                data_xml += get_rubylong_fields_xml(line, 'detail', line_fields_new)
            res[order.id] = get_rublong_data_url(order,data_xml,cr.dbname)

        return res            
    _columns = { 
        'date_plan': fields.date('排程日期',required=True),
        'workcenter_id': fields.many2one('mrp.workcenter','工作中心',required=True),
        'company_id': fields.many2one('res.company','Company',required=True,select=1),
        'rubylong_xml_file':fields.function(_get_rubylong_xml_file, type='text', string='Rubylong xml'),
    }
    
    _defaults = {
        'company_id': lambda self,cr,uid,c: self.pool.get('res.company')._company_default_get(cr, uid, 'mrp.plan.day.print', context=c),
    }
